<template>
  <div class="flex-container">
    <div class="flex-item">
      <div class="header" style="height:128px;">
        <img src="@/assets/img/logo-circle.png" class="system-logo"><span class="title-text">国家医疗保障局医疗保障信息平台</span>
      </div>
    </div>
    <div class="flex-item main">
      <div class="login-panel">
        <img alt="" src="img/login-panel-icon.svg" class="login-panel-icon" :style="styleObj">
        <div class="login-panel-main">
          <el-tabs v-model="activeName">
            <el-tab-pane label="密码登录" name="first">
              <el-form
                ref="loginForm"
                :model="loginForm"
                :rules="loginRules"
                class="login-form"
                auto-complete="on"
                label-position="left"
              >
                <el-form-item prop="username">
                  <span class="svg-container">
                    <svg-icon icon-class="user" />
                  </span>
                  <el-input
                    ref="username"
                    v-model="loginForm.username"
                    placeholder="Username"
                    name="username"
                    type="text"
                    tabindex="1"
                    auto-complete="on"
                  />
                </el-form-item>

                <el-form-item prop="password">
                  <span class="svg-container">
                    <svg-icon icon-class="password" />
                  </span>
                  <el-input
                    :key="passwordType"
                    ref="password"
                    v-model="loginForm.password"
                    :type="passwordType"
                    placeholder="Password"
                    name="password"
                    tabindex="2"
                    auto-complete="on"
                    @keyup.enter.native="handleLogin"
                  />
                  <span class="show-pwd" @click="showPwd">
                    <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
                  </span>
                </el-form-item>

                <el-button
                  :loading="loading"
                  type="primary"
                  style="width:100%;margin-bottom:30px;"
                  @click.native.prevent="handleLogin"
                >Login</el-button>
              </el-form>
            </el-tab-pane>
            <el-tab-pane label="CA登录" name="second">CA登录</el-tab-pane>
            <el-tab-pane label="扫码登录" name="third">扫码登录</el-tab-pane>
          </el-tabs>
        </div>
      </div>
    </div>
    <div class="flex-item">
      <div style="height:100px;line-height:70px;">
        Copyright @2020 All right reserved 版权所有 国家医疗保障局
      </div>
    </div>
  </div>
</template>

<script>
import {
  validUsername
} from '@/utils/validate'

export default {
  name: 'Login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!validUsername(value)) {
        callback(new Error('Please enter the correct user name'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('The password can not be less than 6 digits'))
      } else {
        callback()
      }
    }
    return {
      activeName: 'first',
      styleObj: '',
      loginForm: {
        username: 'admin',
        password: '111111'
      },
      loginRules: {
        username: [{
          required: true,
          trigger: 'blur',
          validator: validateUsername
        }],
        password: [{
          required: true,
          trigger: 'blur',
          validator: validatePassword
        }]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  mounted() {
    this.styleObj = 'left: 16px; opacity: 1;'
  },
  destroyed() {
    // this.styleObj = ''
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('user/login', this.loginForm).then(() => {
            this.$router.push({
              path: this.redirect || '/'
            })
            this.loading = false
          }).catch(() => {
            this.loading = false
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}

</script>
<style lang="scss">
  /* 修复input 背景不协调 和光标变色 */
  /* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

  $bg:#283443;
  $light_gray:#fff;
  $cursor: #fff;

  @supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
    .login-container .el-input input {
      color: $cursor;
    }
  }

  /* reset element-ui css */
  .flex-container {
    .el-input {
      display: inline-block;
      width: 65%;

      input {
        background: transparent;
        border: 0px;
        -webkit-appearance: none;
        border-radius: 0px;
        padding: 12px 5px 12px 15px;
        color: $light_gray;
        caret-color: $cursor;
      }
    }

    .el-form-item {
      border: 1px solid #67c23a;
      // background: rgba(0, 0, 0, 0.1);
      border-radius: 5px;
      color: #454545;
    }
    .el-form-item__content{
      line-height: 32px;
    }
  }

</style>

<style lang="scss" scoped>
  $bg:#2d3a4b;
  $dark_gray:#889aa4;
  $light_gray:#eee;

  .el-container {
    .is-vertical {
      flex-direction: column;
    }
  }

  .main {
    background: url("../../assets/img/login-bg.png");
    background-repeat: no-repeat;
    background-size: cover;
    background-position: 50%;
  }

  .main,
  .footer {
    display: flex;
    justify-content: center;
    align-items: center;
    position: relative;
  }

  .flex-container {
    height: 100%;
    display: flex;
    flex-direction: column;
    text-align: center;
  }

  .flex-item:nth-child(1),
  .flex-item:nth-child(3) {
    flex-grow: 0;
    flex-shrink: 0;
    background-color: #fff;
  }

  .flex-item:nth-child(2) {
    flex-grow: 1;
    flex-shrink: 1;
    background-color: #000;
    overflow-y: auto;
  }

  .center {
    position: fixed;
    top: 50%;
    left: 50%;
    margin-top: -10px;
    margin-left: -24px;
  }

  .header {
    display: flex;
    justify-content: center;
    align-items: center;
    line-height: 52px;
    font-size: 36px;
    color: #333;

    .system-logo {
      width: 50px;
      margin-right: 24px;
    }
  }

  .main {
    .login-panel {
      width: 834px;
      height: 352px;
      box-shadow: 0 0 8px rgba(39, 116, 234, .38), 2px 2px 16px rgba(19, 70, 128, .55);
      background: url('../../assets/img/login-bg-panel.png');
      background-repeat: no-repeat;
      background-size: 100% 100%;
      position: relative;
      overflow: hidden;

      .login-panel-icon{
        background: url("../../assets/img/login-panel-icon.svg");
        background-repeat: no-repeat;
        top: 40px;
        width: 340px;
        height: 272px;
        position: absolute;
        left: -340px;
        opacity: .2;
        transition: left .6s cubic-bezier(.5, 0, 0, 1), opacity .6s;
      }

    }
        .login-panel-main {
    top: 32px;
    width: 400px;
    height: 288px;
    position: absolute;
    right: 20px;
}
.el-tabs{
      padding: 0 20px 0 20px;
}
.el-tabs .el-tabs__header {
    margin-bottom: 16px!important;
}
/deep/ .el-tabs__nav {
    min-width: 100%;
    display: flex;
    .el-tabs__item{
    flex:1;
    font-size: 16px!important;
  }
  }

  }

    .login-form {
          padding: 8px 20px 0 20px;
      // padding: 160px 35px 0;
      margin: 0 auto;
      overflow: hidden;
    }

    .tips {
      font-size: 14px;
      color: #fff;
      margin-bottom: 10px;

      span {
        &:first-of-type {
          margin-right: 16px;
        }
      }
    }

    .svg-container {
      padding: 6px 5px 6px 15px;
      color: $dark_gray;
      vertical-align: middle;
      width: 30px;
      display: inline-block;
    }

    .title-container {
      position: relative;

      .title {
        font-size: 26px;
        color: $light_gray;
        margin: 0px auto 40px auto;
        text-align: center;
        font-weight: bold;
      }
    }

    .show-pwd {
      position: absolute;
      right: 10px;
      top: 7px;
      font-size: 16px;
      color: $dark_gray;
      cursor: pointer;
      user-select: none;
    }

</style>
